package base.dao;

import java.util.ArrayList;
import java.util.List;

public class BaseExample{
    protected String orderByClause;

    protected String table;

    protected boolean distinct;

    protected List<Join> oredJoin;

    protected List<Criteria> oredCriteria;

    protected String groupByClause;

    public BaseExample() {
        oredCriteria = new ArrayList<Criteria>();
        oredJoin = new ArrayList<Join>();
    }

    /**
     * 添加排序条件
     * */
    public void setOrderByClause(String orderByClause) {
        this.orderByClause = orderByClause;
    }

    public String getOrderByClause() {
        return orderByClause;
    }

    public String getTable() {
		return table;
	}

    public List<Join> getOredJoin() {
		return oredJoin;
	}

	public String getGroupByClause() {
		return groupByClause;
	}

	/**
     * 添加分类条件
     * */
	public void setGroupByClause(String groupByClause) {
		this.groupByClause = groupByClause;
	}

	/**
     * 添加表名
     * */
	public void setTable(String table) {
		this.table = table;
	}

	/**
     * 添加左外连接条件
     * @param table 关联表表名
     * @param field 关联字段名
     * */
	public BaseExample leftJoinOn(String table,String field) {
		if (table == null) {
            throw new RuntimeException("Value for table cannot be null");
        }
		if (field == null) {
            throw new RuntimeException("Value for field cannot be null");
        }
		this.oredJoin.add(new Join("LEFT",table, field, null, null, null));
		return (BaseExample)this;
	}

	/**
     * 添加左外连接条件
     * @param table 关联表表名
     * @param field 关联字段名
     * @param param 其他外联sql条件，字符串需加单引号
     * */
	public BaseExample leftJoinOn(String table,String field,Object param) {
		if (table == null) {
            throw new RuntimeException("Value for table cannot be null");
        }
		if (field == null) {
            throw new RuntimeException("Value for field cannot be null");
        }
		this.oredJoin.add(new Join("LEFT", table, field, null, null, param));
		return (BaseExample)this;
	}

	/**
     * 添加左外连接条件
     * @param table 关联表表名
     * @param field 关联字段名
     * @param tofield 对应查询表的字段名
     * */
	public BaseExample leftJoinOn(String table,String field,String tofield) {
		if (table == null) {
            throw new RuntimeException("Value for table cannot be null");
        }
		if (field == null) {
            throw new RuntimeException("Value for field cannot be null");
        }
		this.oredJoin.add(new Join("LEFT", table, field, null, tofield, null));
		return (BaseExample)this;
	}

	/**
     * 添加左外连接条件
     * @param table 关联表表名
     * @param field 关联字段名
     * @param tofield 对应查询表的字段名
     * @param param 其他外联sql条件，字符串需加单引号
     * */
	public BaseExample leftJoinOn(String table,String field,String tofield,Object param) {
		if (table == null) {
            throw new RuntimeException("Value for table cannot be null");
        }
		if (field == null) {
            throw new RuntimeException("Value for field cannot be null");
        }
		this.oredJoin.add(new Join("LEFT", table, field, null, tofield, param));
		return (BaseExample)this;
	}

	/**
     * 添加左外连接条件
     * @param table 关联表表名
     * @param field 关联字段名
     * @param totable 对应表表名
     * @param tofield 对应表的字段名
     * */
	public BaseExample leftJoinOn(String table,String field,String totable,String tofield) {
		if (table == null) {
            throw new RuntimeException("Value for table cannot be null");
        }
		if (field == null) {
            throw new RuntimeException("Value for field cannot be null");
        }
		this.oredJoin.add(new Join("LEFT", table, field, totable, tofield, null));
		return (BaseExample)this;
	}

	/**
     * 添加左外连接条件
     * @param table 关联表表名
     * @param field 关联字段名
     * @param totable 对应表表名
     * @param tofield 对应表的字段名
     * @param param 其他外联sql条件，字符串需加单引号
     * */
	public BaseExample leftJoinOn(String table,String field,String totable,String tofield,Object param) {
		if (table == null) {
            throw new RuntimeException("Value for table cannot be null");
        }
		if (field == null) {
            throw new RuntimeException("Value for field cannot be null");
        }
		this.oredJoin.add(new Join("LEFT", table, field, totable, tofield, param));
		return (BaseExample)this;
	}

	/**
	 * 添加右外连接条件
	 * @param table 关联表表名
     * @param field 关联字段名
	 * */
	public BaseExample rightJoinOn(String table,String field) {
		if (table == null) {
            throw new RuntimeException("Value for table cannot be null");
        }
		if (field == null) {
            throw new RuntimeException("Value for field cannot be null");
        }
		this.oredJoin.add(new Join("RIGHT", table, field, null, null, null));
		return (BaseExample)this;
	}

	/**
	 * 添加右外连接条件
	 * @param table 关联表表名
     * @param field 关联字段名
     * @param param 其他外联sql条件，字符串需加单引号
	 * */
	public BaseExample rightJoinOn(String table,String field,Object param) {
		if (table == null) {
            throw new RuntimeException("Value for table cannot be null");
        }
		if (field == null) {
            throw new RuntimeException("Value for field cannot be null");
        }
		this.oredJoin.add(new Join("RIGHT", table, field, null, null, param));
		return (BaseExample)this;
	}

	/**
	 * 添加右外连接条件
	 * @param table 关联表表名
     * @param field 关联字段名
     * @param tofield 对应查询表的字段名
	 * */
	public BaseExample rightJoinOn(String table,String field,String tofield) {
		if (table == null) {
            throw new RuntimeException("Value for table cannot be null");
        }
		if (field == null) {
            throw new RuntimeException("Value for field cannot be null");
        }
		this.oredJoin.add(new Join("RIGHT", table, field, null, tofield, null));
		return (BaseExample)this;
	}

	/**
	 * 添加右外连接条件
	 * @param table 关联表表名
     * @param field 关联字段名
     * @param tofield 对应查询表的字段名
     * @param param 其他外联sql条件，字符串需加单引号
	 * */
	public BaseExample rightJoinOn(String table,String field,String tofield,Object param) {
		if (table == null) {
            throw new RuntimeException("Value for table cannot be null");
        }
		if (field == null) {
            throw new RuntimeException("Value for field cannot be null");
        }
		this.oredJoin.add(new Join("RIGHT", table, field, null, tofield, param));
		return (BaseExample)this;
	}

	/**
	 * 添加右外连接条件
	 * @param table 关联表表名
     * @param field 关联字段名
     * @param totable 对应表表名
     * @param tofield 对应查询表的字段名
	 * */
	public BaseExample rightJoinOn(String table,String field,String totable,String tofield) {
		if (table == null) {
            throw new RuntimeException("Value for table cannot be null");
        }
		if (field == null) {
            throw new RuntimeException("Value for field cannot be null");
        }
		this.oredJoin.add(new Join("RIGHT", table, field, totable, tofield, null));
		return (BaseExample)this;
	}

	/**
	 * 添加右外连接条件
	 * @param table 关联表表名
     * @param field 关联字段名
     * @param totable 对应表表名
     * @param tofield 对应查询表的字段名
     * @param param 其他外联sql条件，字符串需加单引号
	 * */
	public BaseExample rightJoinOn(String table,String field,String totable,String tofield,Object param) {
		if (table == null) {
            throw new RuntimeException("Value for table cannot be null");
        }
		if (field == null) {
            throw new RuntimeException("Value for field cannot be null");
        }
		this.oredJoin.add(new Join("RIGHT", table, field, totable, tofield, param));
		return (BaseExample)this;
	}

	/**
	 * 添加外连接条件
	 * @param table 关联表表名
     * @param field 关联字段名
	 * */
	public BaseExample joinOn(String table,String field) {
		if (table == null) {
            throw new RuntimeException("Value for table cannot be null");
        }
		if (field == null) {
            throw new RuntimeException("Value for field cannot be null");
        }
		this.oredJoin.add(new Join("", table, field, null, null, null));
		return (BaseExample)this;
	}

	/**
	 * 添加外连接条件
	 * @param table 关联表表名
     * @param field 关联字段名
     * @param param 其他外联sql条件，字符串需加单引号
	 * */
	public BaseExample joinOn(String table,String field,Object param) {
		if (table == null) {
            throw new RuntimeException("Value for table cannot be null");
        }
		if (field == null) {
            throw new RuntimeException("Value for field cannot be null");
        }
		this.oredJoin.add(new Join("", table, field, null, null, param));
		return (BaseExample)this;
	}

	/**
	 * 添加外连接条件
	 * @param table 关联表表名
     * @param field 关联字段名
     * @param tofield 对应查询表的字段名
	 * */
	public BaseExample joinOn(String table,String field,String tofield) {
		if (table == null) {
            throw new RuntimeException("Value for table cannot be null");
        }
		if (field == null) {
            throw new RuntimeException("Value for field cannot be null");
        }
		this.oredJoin.add(new Join("", table, field, null, tofield, null));
		return (BaseExample)this;
	}

	/**
	 * 添加外连接条件
	 * @param table 关联表表名
     * @param field 关联字段名
     * @param tofield 对应查询表的字段名
     * @param param 其他外联sql条件，字符串需加单引号
	 * */
	public BaseExample joinOn(String table,String field,String tofield,Object param) {
		if (table == null) {
            throw new RuntimeException("Value for table cannot be null");
        }
		if (field == null) {
            throw new RuntimeException("Value for field cannot be null");
        }
		this.oredJoin.add(new Join("", table, field, null, tofield, param));
		return (BaseExample)this;
	}

	/**
	 * 添加外连接条件
	 * @param table 关联表表名
     * @param field 关联字段名
     * @param totable 对应表表名
     * @param tofield 对应查询表的字段名
	 * */
	public BaseExample joinOn(String table,String field,String totable,String tofield) {
		if (table == null) {
            throw new RuntimeException("Value for table cannot be null");
        }
		if (field == null) {
            throw new RuntimeException("Value for field cannot be null");
        }
		this.oredJoin.add(new Join("", table, field, totable, tofield, null));
		return (BaseExample)this;
	}

	/**
	 * 添加外连接条件
	 * @param table 关联表表名
     * @param field 关联字段名
     * @param totable 对应表表名
     * @param tofield 对应查询表的字段名
     * @param param 其他外联sql条件，字符串需加单引号
	 * */
	public BaseExample joinOn(String table,String field,String totable,String tofield,Object param) {
		if (table == null) {
            throw new RuntimeException("Value for table cannot be null");
        }
		if (field == null) {
            throw new RuntimeException("Value for field cannot be null");
        }
		this.oredJoin.add(new Join("", table, field, totable, tofield, param));
		return (BaseExample)this;
	}

	public void setDistinct(boolean distinct) {
        this.distinct = distinct;
    }

    public boolean isDistinct() {
        return distinct;
    }

	public List<Criteria> getOredCriteria() {
        return oredCriteria;
    }

    public void or(Criteria criteria) throws CloneNotSupportedException {
    	Criteria newcriteria = (Criteria) criteria.clone();
        oredCriteria.add(newcriteria);
    }

    /**
     * 添加异或条件
     * @return
     */
    public Criteria or() {
        Criteria criteria = createCriteriaInternal();
        oredCriteria.add(criteria);
        return criteria;
    }
    
    /**
     * 在指定异或条件中追加条件
     * @param index
     * @return
     * @throws CloneNotSupportedException 
     */
    public Criteria or(int index) {
    	if(oredCriteria.size() <= index){
    		throw new RuntimeException("Criteria's size is letter than the value");
    	}
        Criteria criteria = oredCriteria.get(index);
        return criteria;
    }

    public Criteria createCriteria() {
        Criteria criteria = createCriteriaInternal();
        if (oredCriteria.size() == 0) {
            oredCriteria.add(criteria);
        }
        return criteria;
    }

    protected Criteria createCriteriaInternal() {
        Criteria criteria = new Criteria();
        return criteria;
    }

    /**
     * 清空用例中所有的条件
     * */
    public void clear() {
        oredCriteria.clear();
        oredJoin.clear();
        orderByClause = null;
        distinct = false;
        groupByClause = null;
    }
    
    /**
     * 清空用例中的or条件
     * */
    public void clearOR() {
    	oredCriteria.clear();
    }

    public static class Join {
    	private String style;

    	private String table;

        private String field;

        private String totable;

        private String tofield;

        private Object param;

		public String getStyle() {
			return style;
		}

		public Object getParam() {
			return param;
		}

		public void setParam(Object param) {
			this.param = param;
		}

		public String getTable() {
			return table;
		}

		public String getField() {
			return field;
		}

		public String getTotable() {
			return totable;
		}

		public String getTofield() {
			return tofield;
		}

		protected Join(String style,String table,String field,String totable,String tofield,Object param) {
			super();
			this.style = style;
			this.table = table;
			this.field = field;
			this.totable = totable;
			this.tofield = tofield;
			this.param = param;
		}

    }

    protected abstract static class GeneratedCriteria{
        protected List<Criterion> criteria;

        protected GeneratedCriteria() {
            super();
            criteria = new ArrayList<Criterion>();
        }

        public boolean isValid() {
            return criteria.size() > 0;
        }

        public List<Criterion> getAllCriteria() {
            return criteria;
        }

        public List<Criterion> getCriteria() {
            return criteria;
        }

        protected void addCriterion(String tableName, String condition) {
            if (condition == null) {
                throw new RuntimeException("Value for condition cannot be null");
            }
            criteria.add(new Criterion(tableName, condition));
        }

        protected void addCriterion(String tableName, String condition, Object value, String property) {
            if (value == null) {
                throw new RuntimeException("Value for " + property + " cannot be null");
            }
            criteria.add(new Criterion(tableName, condition, value));
        }

        protected void addCriterion(String tableName, String condition, Object value1, Object value2, String property) {
            if (value1 == null || value2 == null) {
                throw new RuntimeException("Between values for " + property + " cannot be null");
            }
            criteria.add(new Criterion(tableName, condition, value1, value2));
        }

        /**
         * 添加为空条件
         * @param field 字段名
         * */
        public Criteria andIsNull(String field) {
            addCriterion(null, field + " IS NULL");
            return (Criteria) this;
        }
        
        /**
         * 添加为空条件
         * @param table 表名
         * @param field 字段名
         * */
        public Criteria andIsNull(String table, String field) {
            addCriterion(table, field + " IS NULL");
            return (Criteria) this;
        }

        /**
         * 添加非空条件
         * @param field 字段名
         * */
        public Criteria andIsNotNull(String field) {
            addCriterion(null, field + " IS NOT NULL");
            return (Criteria) this;
        }
        
        /**
         * 添加非空条件
         * @param table 表名
         * @param field 字段名
         * */
        public Criteria andIsNotNull(String table, String field) {
            addCriterion(table, field + " IS NOT NULL");
            return (Criteria) this;
        }

        /**
         * 添加等于条件
         * @param field 字段名
         * @param value 值
         * */
        public Criteria andEqualTo(String field,Object value) {
            addCriterion(null, field + " =", value, field);
            return (Criteria) this;
        }
        
        /**
         * 添加等于条件
         * @param table 表名
         * @param field 字段名
         * @param value 值
         * */
        public Criteria andEqualTo(String table, String field,Object value) {
            addCriterion(table, field + " =", value, field);
            return (Criteria) this;
        }

        /**
         * 添加不等于条件
         * @param field 字段名
         * @param value 值
         * */
        public Criteria andNotEqualTo(String field,Object value) {
            addCriterion(null, field + " <>", value, field);
            return (Criteria) this;
        }
        
        /**
         * 添加不等于条件
         * @param table 表名
         * @param field 字段名
         * @param value 值
         * */
        public Criteria andNotEqualTo(String table, String field,Object value) {
            addCriterion(table, field + " <>", value, field);
            return (Criteria) this;
        }

        /**
         * 添加大于条件
         * @param field 字段名
         * @param value 值
         * */
        public Criteria andGreaterThan(String field,Object value) {
            addCriterion(null, field + " >", value, field);
            return (Criteria) this;
        }
        
        /**
         * 添加大于条件
         * @param table 表名
         * @param field 字段名
         * @param value 值
         * */
        public Criteria andGreaterThan(String table, String field,Object value) {
            addCriterion(table, field + " >", value, field);
            return (Criteria) this;
        }

        /**
         * 添加大于等于条件
         * @param field 字段名
         * @param value 值
         * */
        public Criteria andGreaterThanOrEqualTo(String field,Object value) {
            addCriterion(null, field + " >=", value, field);
            return (Criteria) this;
        }
        
        /**
         * 添加大于等于条件
         * @param table 表名
         * @param field 字段名
         * @param value 值
         * */
        public Criteria andGreaterThanOrEqualTo(String table, String field,Object value) {
            addCriterion(table, field + " >=", value, field);
            return (Criteria) this;
        }

        /**
         * 添加小于条件
         * @param field 字段名
         * @param value 值
         * */
        public Criteria andLessThan(String field,Object value) {
            addCriterion(null, field + " <", value, field);
            return (Criteria) this;
        }
        
        /**
         * 添加小于条件
         * @param table 表名
         * @param field 字段名
         * @param value 值
         * */
        public Criteria andLessThan(String table, String field,Object value) {
            addCriterion(table, field + " <", value, field);
            return (Criteria) this;
        }

        /**
         * 添加小于等于条件
         * @param field 字段名
         * @param value 值
         * */
        public Criteria andLessThanOrEqualTo(String field,Object value) {
            addCriterion(null, field + " <=", value, field);
            return (Criteria) this;
        }
        
        /**
         * 添加小于等于条件
         * @param table 表名
         * @param field 字段名
         * @param value 值
         * */
        public Criteria andLessThanOrEqualTo(String table, String field,Object value) {
            addCriterion(table, field + " <=", value, field);
            return (Criteria) this;
        }

        /**
         * 添加相似条件
         * @param field 字段名
         * @param value 值
         * */
        public Criteria andLike(String field,Object value) {
            addCriterion(null, field + " LIKE", "%" + (value==null?"":value) + "%", field);
            return (Criteria) this;
        }
        
        /**
         * 添加相似条件
         * @param table 表名
         * @param field 字段名
         * @param value 值
         * */
        public Criteria andLike(String table, String field,Object value) {
            addCriterion(table, field + " LIKE", "%" + (value==null?"":value) + "%", field);
            return (Criteria) this;
        }

        /**
         * 添加不相似条件
         * @param field 字段名
         * @param value 值
         * */
        public Criteria andNotLike(String field,Object value) {
            addCriterion(null, field + " NOT LIKE", "%" + (value==null?"":value) + "%", field);
            return (Criteria) this;
        }
        
        /**
         * 添加不相似条件
         * @param table 表名
         * @param field 字段名
         * @param value 值
         * */
        public Criteria andNotLike(String table, String field,Object value) {
            addCriterion(table, field + " NOT LIKE", "%" + (value==null?"":value) + "%", field);
            return (Criteria) this;
        }

        /**
         * 添加包含于条件
         * @param field 字段名
         * @param values 值(列表)
         * */
        public Criteria andIn(String field,List<Object> values) {
            addCriterion(null, field + " IN", values, field);
            return (Criteria) this;
        }
        
        /**
         * 添加包含于条件
         * @param table 表名
         * @param field 字段名
         * @param values 值(列表)
         * */
        public Criteria andIn(String table, String field,List<Object> values) {
            addCriterion(table, field + " IN", values, field);
            return (Criteria) this;
        }

        /**
         * 添加不包含于条件
         * @param field 字段名
         * @param values 值(列表)
         * */
        public Criteria andNotIn(String field,List<Object> values) {
            addCriterion(null, field + " NOT IN", values, field);
            return (Criteria) this;
        }
        
        /**
         * 添加不包含于条件
         * @param table 表名
         * @param field 字段名
         * @param values 值(列表)
         * */
        public Criteria andNotIn(String table, String field,List<Object> values) {
            addCriterion(table, field + " NOT IN", values, field);
            return (Criteria) this;
        }

        /**
         * 添加介于条件
         * @param field 字段名
         * @param value1 区间值左
         * @param value2 区间值右
         * */
        public Criteria andBetween(String field,Object value1, Object value2) {
            addCriterion(null, field + " BETWEEN", value1, value2, field);
            return (Criteria) this;
        }
        
        /**
         * 添加介于条件
         * @param table 表名
         * @param field 字段名
         * @param value1 区间值左
         * @param value2 区间值右
         * */
        public Criteria andBetween(String table, String field,Object value1, Object value2) {
            addCriterion(table, field + " BETWEEN", value1, value2, field);
            return (Criteria) this;
        }

        /**
         * 添加不介于条件
         * @param field 字段名
         * @param value1 区间值左
         * @param value2 区间值右
         * */
        public Criteria andNotBetween(String field,Object value1, Object value2) {
            addCriterion(null, field + " NOT BETWEEN", value1, value2, field);
            return (Criteria) this;
        }
        
        /**
         * 添加不介于条件
         * @param table 表名
         * @param field 字段名
         * @param value1 区间值左
         * @param value2 区间值右
         * */
        public Criteria andNotBetween(String table, String field,Object value1, Object value2) {
            addCriterion(table, field + " NOT BETWEEN", value1, value2, field);
            return (Criteria) this;
        }

    }

    public static class Criteria extends GeneratedCriteria implements Cloneable{

        protected Criteria() {
            super();
        }
        
        @Override
        protected Object clone() throws CloneNotSupportedException {
            Object o=null;   
            try{   
                o= super.clone();   
            }catch(CloneNotSupportedException e){   
                System.out.println(e.toString());   
            }   
            return o;
        }
    }

    public static class Criterion {
        private String condition;

        private Object value;

        private Object secondValue;

        private boolean noValue;

        private boolean singleValue;

        private boolean betweenValue;

        private boolean listValue;
        
        private boolean defultValue;

        private String typeHandler;

        public String getCondition() {
            return condition;
        }

        public Object getValue() {
            return value;
        }

        public Object getSecondValue() {
            return secondValue;
        }

        public boolean isNoValue() {
            return noValue;
        }

        public boolean isSingleValue() {
            return singleValue;
        }

        public boolean isBetweenValue() {
            return betweenValue;
        }

        public boolean isListValue() {
            return listValue;
        }

        public boolean isDefultValue() {
			return defultValue;
		}

		public String getTypeHandler() {
            return typeHandler;
        }

        protected Criterion(String table, String condition) {
            super();
            this.condition = condition;
            this.typeHandler = null;
            this.noValue = true;
            this.defultValue = true;
            if(table != null){
            	this.condition = table + "." + condition;
            	this.defultValue = false;
            }
        }

        protected Criterion(String table, String condition, Object value, String typeHandler) {
            super();
            this.condition = condition;
            this.value = value;
            this.typeHandler = typeHandler;
            this.defultValue = true;
            if (value instanceof List<?>) {
                this.listValue = true;
            } else {
                this.singleValue = true;
            }
            if(table != null){
            	this.condition = table + "." + condition;
            	this.defultValue = false;
            }
        }

        protected Criterion(String table, String condition, Object value) {
            this(table, condition, value, null);
        }

        protected Criterion(String table, String condition, Object value, Object secondValue, String typeHandler) {
            super();
            this.condition = condition;
            this.value = value;
            this.secondValue = secondValue;
            this.typeHandler = typeHandler;
            this.betweenValue = true;
            this.defultValue = true;
            if(table != null){
            	this.condition = table + "." + condition;
            	this.defultValue = false;
            }
        }

        protected Criterion(String table, String condition, Object value, Object secondValue) {
            this(table, condition, value, secondValue, null);
        }
    }
}